package org.zju.demo.shoppingdemo.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.tsz.afinal.FinalBitmap;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.application.ShoppingApplication;
import org.zju.demo.shoppingdemo.model.Product;

import java.util.List;

/**
 * Created by ZJDX on 2015/9/13.
 */
public class CartItemListAdapter extends BaseAdapter {
    private Activity activity;
    private List<Product> productList;
    private FinalBitmap imageLoader;

    public CartItemListAdapter(Activity activity, List<Product> products) {
        this.activity = activity;
        this.productList = products;
        imageLoader = ((ShoppingApplication)activity.getApplication()).getFinalBitmap();
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return productList.size();
    }

    public Object getItem(int position) {
        return productList.get(position);
    }
    static class ViewHolder {
        public TextView productTitle;
        public ImageView productImg;
        public TextView productNumber;
        public TextView productPrice;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        Product product = productList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.cart_cell, null);
            viewHolder = new ViewHolder();
            viewHolder.productImg = (ImageView)convertView.findViewById(R.id.cartImage);
            viewHolder.productNumber = (TextView)convertView.findViewById(R.id.cartNumber);
            viewHolder.productTitle = (TextView)convertView.findViewById(R.id.cartTitle);
            viewHolder.productPrice = (TextView)convertView.findViewById(R.id.cartPrice);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Log.e("cartItemlist", " " + product.getNumber());
        Log.e("cartItemList", viewHolder.productNumber.toString());
        viewHolder.productTitle.setText(product.getProductName());
        viewHolder.productNumber.setText(Integer.toString(product.getNumber()));
        viewHolder.productPrice.setText(Float.toString(product.getPrice()));
        Log.e("cartItemList", product.getImageUrl());
        imageLoader.display(viewHolder.productImg, "http://10.214.143.25:8080/" + product.getImageUrl());
        return convertView;
    }
}
